package spit;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


public class Card extends Component {
	
	BufferedImage cardImg;
	String fileLocationStart = "src/res/";
	String fileLocationEnd = ".gif";
	
	String rank;
	String suit;
	
	int rankValue;
		
	boolean flippedToFront;
	
	public void paint(Graphics g)
	{
		g.drawImage(cardImg, 0, 0, null);
	}
	
	public Card()
	{
		  try {
	          cardImg = ImageIO.read(new File(fileLocationStart + "backImage" + fileLocationEnd));
	       } catch (IOException e) {
	    	   System.out.print("What?");
	       }	
	}
	
	public Card(String aRank, String aSuit)
	{		
		rank = aRank;
		suit = aSuit;
		
		flippedToFront = true;
		flip();
		
		setRankValue();
		
		
	}
	
	public void flip()
	{
		if ( flippedToFront == true)
		{
			flippedToFront = false;
			
			try {
		         cardImg = ImageIO.read(new File(fileLocationStart + "backImage" + fileLocationEnd));
		         } 
			catch (IOException e) {
		    	  System.out.print("What?");
		      }	
			
		}
		else
		{
			flippedToFront = true;
			
			try {
		         cardImg = ImageIO.read(new File(fileLocationStart + rank + suit + fileLocationEnd));
		         } 
			catch (IOException e) {
		    	  System.out.print("What?");
		      }	
		}
	}
	
	public Dimension getPreferredSize() {
        if (cardImg == null) {
             return new Dimension(100,100);
        } else {
           return new Dimension(cardImg.getWidth(null), cardImg.getHeight(null));
       }
    }
	
	public int getHeight()
	{
		return cardImg.getHeight();
	}
	
	public int getWidth()
	{
		return cardImg.getWidth();
	}
	
	public String getRank()
	{
		return rank;
	}
	
	public String getSuit()
	{
		return suit;
	}
	
	public String toString()
	{
		return rank + " and " + suit;
	}
	
	public boolean isFlippedToFront()
	{
		return flippedToFront;
	}
	
	public boolean rankEquals(Card aCard)
	{
		Card card = aCard;
		
		if ( card.getRank().equals(this.getRank()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void setRankValue()
	{
		if ( rank.matches("[0-9]+"))
		{
			rankValue = Integer.parseInt(rank);
		}
		else if ( rank.equals("Jack"))
		{
			rankValue = 11;
		}
		else if ( rank.equals("Queen"))
		{
			rankValue = 12;
		}
		else if ( rank.equals("King"))
		{
			rankValue = 13; 
		}
		else if ( rank.equals("Ace"))
		{
			rankValue = 1;
		}
	}
	
	public int getRankValue()
	{
		return rankValue;
	}
	
	public boolean rankNextTo(Card aCard)
	{
		Card card = aCard;
		Integer rankValue;
			
		if ( card.getRankValue() == this.getRankValue() + 1 || card.getRankValue() == this.getRankValue() - 1 || 
				card.getRankValue() == this.getRankValue() + 12 || card.getRankValue() == this.getRankValue() - 12)
		{
			return true;
		}
		else
		{
			return false;
		}	
		
	}

}
